import logging
from community import GetCommunityName
from house_info import GetInfo
from concurrent.futures import ThreadPoolExecutor

# 设置日志格式
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
# 设置时间格式
DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p"

logging.basicConfig(filename='test.log', level=logging.INFO, format=LOG_FORMAT, datefmt=DATE_FORMAT)

def get_info(community_name):
    info = GetInfo(community_name)
    # 获取挂牌房源id
    gpfyids = info.get_gpfyid()
    # 通过挂牌房源id获取房源详情信息并存入数据库
    if gpfyids:
        logging.info("获取小区<%s>成功" % community_name)
        for gpfyid in gpfyids:
            info.get_detail_content(gpfyid)
            info.get_image_content(gpfyid)
    else:
        logging.info("获取小区<%s>未成功" % community_name)

def main():
    # 获取小区名称
    app = GetCommunityName()
    community_name_list_generator = app.run()
    # 多线程获取小区某二手房挂牌信息
    for community_name_list in community_name_list_generator:
        with ThreadPoolExecutor(len(community_name_list)) as executor:
            executor.map(get_info, community_name_list)

if __name__ == "__main__":
    main()